package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.SafePayBookPreApplyRecordVo;
import com.net.xpay.common.domain.SafePayBookPreApplyRecord;
import com.net.xpay.common.enums.config.CommonSysConfigKey;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.manager.SafePayBookPreApplyRecordManager;
import com.net.xpay.common.model.SafePayBookPreApplyModel;
import com.net.xpay.common.model.search.SafePayBookPreApplyRecordSearchModel;
import com.net.xpay.common.service.SafePayBookPreApplyService;
import com.net.xpay.core.model.IdModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 */
@RestController
@Slf4j
@RequestMapping("/admin/safe-pay-book-pre-apply-record")
@Api(tags = "后台/记账本开通", description = "后台 记账本开通")
public class AdminSafePayBookPreApplyRecordController {

    @Autowired
    private SafePayBookPreApplyService safePayBookPreApplyService;

    @Autowired
    private SafePayBookPreApplyRecordManager safePayBookPreApplyRecordManager;

    @Autowired
    private CommonSysConfigHelper commonSysConfigHelper;

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/search")
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-book-pre-apply-record::search')")
    public ResultPagedList<SafePayBookPreApplyRecordVo> search(@RequestBody SafePayBookPreApplyRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        String shortDomain = commonSysConfigHelper.getString(CommonSysConfigKey.SHORT_DOMAIN);
        List<SafePayBookPreApplyRecord> records = safePayBookPreApplyRecordManager.search(searchModel);
        List<SafePayBookPreApplyRecordVo> vos = BeanCopierUtil.batchCopy(SafePayBookPreApplyRecord.class, SafePayBookPreApplyRecordVo.class, records);
        for (SafePayBookPreApplyRecordVo vo : vos) {
            vo.setApplyShortUrl("https://" + shortDomain + "/s/a/" + vo.getApplyToken());
        }
        long count = safePayBookPreApplyRecordManager.searchCount(searchModel);

        return new ResultPagedList<>(vos, count, searchModel);
    }


    @ApiOperation("记账本开通")
    @PostMapping("/apply")
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-book-pre-apply-record::apply')")
    public ResultData<Boolean> applyBook(@RequestBody SafePayBookPreApplyModel model) {
        safePayBookPreApplyService.applySafePayBook(model);
        return new ResultData<>(true);
    }


    @ApiOperation("记账本挂载")
    @PostMapping("/mount")
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-book-pre-apply-record::mount')")
    public ResultData<Boolean> mountedBook(@RequestBody IdModel idModel) {
        safePayBookPreApplyService.mountBook(idModel.getId());
        return new ResultData<>(true);
    }

}
